/**
 ******************************************************************************
 * @file    string_generate.c
 * @author  Li Zhenxu
 * @version V1.0.0
 * @date    2023/1/16
 * @brief   生成可用于串口DMA发送的字符串，格式为串口调试助手所能识别的格式
 ******************************************************************************
 * @attention
 * 
 ******************************************************************************
 */

#include "string_generate.h"

void String_Generate(uint8_t Sendbuf[] , int parameter_num , uint8_t name[][10] , ...)
{
    va_list ap;
    int count = 0 ;
    va_start(ap , name); //va_start作用是告诉下面的va_arg从哪个参数开始是可变参数
    double val[parameter_num];
    int integer[parameter_num];
    double decimal[parameter_num];

    //用于将传入的可变参数放入对应的数组，供之后代码处理使用。
    //va_arg(va_list ap , type) 作用是 检索函数参数列表中类型为 type 的下一个参数
    for(count = 0 ; count < parameter_num ; count++)
    {
        val[count] = va_arg(ap , double);
    }

    va_end(ap);

    //进行字符串数据生成(部分代码参考了姚学长的)
    int position = 0; //用于记录发送字符串字节数
    int i , p = 0;
    for (i = 0; i < parameter_num; Sendbuf[position++] = ',')
    {
        for (p = 0 ; p < 10 ; p++)
        {
            if (name[i][p] != 0)
            {
                Sendbuf[position++] = name[i][p];
            }
            else break;
        }
        
        Sendbuf[position++] = '=';

        if (val[i] < 0)
        {
            Sendbuf[position++] = '-';
            integer[i] = -(int)val[i];
            decimal[i] = -val[i] - integer[i];
        }
        else
        {
            integer[i] = (int)val[i];
            decimal[i] = val[i] - integer[i];
        }
        
        //整数处理
        int n = integer[i];
        int k = 0 , m = 0;
        int integer_buf[3] = {0};

        if (n == 0)
        {
            Sendbuf[position++] = '0';
        }
        else
        {
            while (n != 0)
            {
                integer_buf[k] = n % 10;
                n /= 10;
                k++; 
            }
        }
        

        //遍历integer_buf数组，由数组最后一位开始输出
        for (m = k - 1 ; m >= 0 ; m--)
        {
            switch (integer_buf[m])
            {
            case 0: Sendbuf[position++] = '0';
                break;
            case 1: Sendbuf[position++] = '1';
                break;
            case 2: Sendbuf[position++] = '2';
                break;
            case 3: Sendbuf[position++] = '3';
                break;
            case 4: Sendbuf[position++] = '4';
                break;
            case 5: Sendbuf[position++] = '5';
                break;
            case 6: Sendbuf[position++] = '6';
                break;
            case 7: Sendbuf[position++] = '7';
                break;
            case 8: Sendbuf[position++] = '8';
                break;
            case 9: Sendbuf[position++] = '9';
                break;
            }
        }

        Sendbuf[position++] = '.';//整数后的小数点

        //小数处理
        int a;
        for (a = 0; a < 3; a++)
        {
            Sendbuf[position++] = (int)(decimal[i] * 10) + '0';
            decimal[i] *= 10;
            decimal[i] -= (int)decimal[i]; 
        }

        i++;
    }
    
    //根据串口协议最后发送空格加回车
    Sendbuf[position++] = '\r';
    Sendbuf[position++] = '\n';

    //DMA发送
    HAL_UART_Transmit_DMA(&huart1, Sendbuf, position);
}

void Rx_PID_Param(UART_HandleTypeDef *huart , uint8_t Rx_Data[] , int len , float* pid_param)
{
    int i = 0 , m = 0 , n = 0 , k = 0;
    uint8_t buf[6][10];

    //跳过电机编号
    m += 2;

    //数据读取
    for (i = 0; i < 6; i++)
    {
        for (n = 0; m < len; m++)
        {
            if (Rx_Data[m] == ',')
            {
                m++;
                break;
            }

            buf[i][n] = Rx_Data[m];
            n++;
        }
    }

    //数据存放
    for (k = 0; k < 6; k++)
    {
        pid_param[k] = (float)atof(buf[k]);
    }
    
}
